<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. 新建项目首先新建一个工程，并且勾选 Include C++ Support 即可得到一个基于CMake的模板工程。">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Android-Studio使用FFmpeg">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/FFmpeg/Android-Studio使用FFmpeg/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. 新建项目首先新建一个工程，并且勾选 Include C++ Support 即可得到一个基于CMake的模板工程。">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-c29e03893724e577.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-b4a4a4b51151a462.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-d1168eca09143bc6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T11:04:43.402Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android-Studio使用FFmpeg">
<meta name="twitter:description" content="####1. 新建项目首先新建一个工程，并且勾选 Include C++ Support 即可得到一个基于CMake的模板工程。">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/3110861-c29e03893724e577.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/FFmpeg/Android-Studio使用FFmpeg/"/>





  <title>Android-Studio使用FFmpeg | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/FFmpeg/Android-Studio使用FFmpeg/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android-Studio使用FFmpeg</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. 新建项目<br>首先新建一个工程，并且勾选 Include C++ Support 即可得到一个基于CMake的模板工程。<br><img src="http://upload-images.jianshu.io/upload_images/3110861-c29e03893724e577.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"><br><a id="more"></a></p>
<p>####2. 将编译FFmpeg生成的头文件和动态库拷贝到app/libs目录下，拷贝完后的目录结构如下所示：<br><img src="http://upload-images.jianshu.io/upload_images/3110861-b4a4a4b51151a462.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图2.png"></p>
<p>####3. 配置build.gradle<br>在app目录下的build.gradle文件中，android节点下的defaultConfig节点下添加<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">//        APP启动之后一直crash，原因就是没有找到我们在java文件里load的动态库</span><br><span class="line">        sourceSets &#123;</span><br><span class="line">            main &#123;</span><br><span class="line">                jniLibs.srcDirs = [&apos;libs&apos;]</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure></p>
<p>在externalNativeBuild节点下添加<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">//          指定abiFilters，因为AndroidStudio默认会编译所有架构的动态库</span><br><span class="line">            ndk&#123;</span><br><span class="line">                abiFilters &quot;armeabi&quot;</span><br><span class="line">            &#125;</span><br></pre></td></tr></table></figure></p>
<p>修改完成后的android节点内容如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">android &#123;</span><br><span class="line">    compileSdkVersion 26</span><br><span class="line">    buildToolsVersion &quot;27.0.3&quot;</span><br><span class="line">    defaultConfig &#123;</span><br><span class="line">        applicationId &quot;com.mazaiting.ffmpegtest&quot;</span><br><span class="line">        minSdkVersion 19</span><br><span class="line">        targetSdkVersion 26</span><br><span class="line">        versionCode 1</span><br><span class="line">        versionName &quot;1.0&quot;</span><br><span class="line">        testInstrumentationRunner &quot;android.support.test.runner.AndroidJUnitRunner&quot;</span><br><span class="line">//        APP启动之后一直crash，原因就是没有找到我们在java文件里load的动态库</span><br><span class="line">        sourceSets &#123;</span><br><span class="line">            main &#123;</span><br><span class="line">                jniLibs.srcDirs = [&apos;libs&apos;]</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        externalNativeBuild &#123;</span><br><span class="line">            cmake &#123;</span><br><span class="line">                cppFlags &quot;-fexceptions&quot;</span><br><span class="line">            &#125;</span><br><span class="line">//          指定abiFilters，因为AndroidStudio默认会编译所有架构的动态库</span><br><span class="line">            ndk&#123;</span><br><span class="line">                abiFilters &quot;armeabi&quot;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    buildTypes &#123;</span><br><span class="line">        release &#123;</span><br><span class="line">            minifyEnabled false</span><br><span class="line">            proguardFiles getDefaultProguardFile(&apos;proguard-android.txt&apos;), &apos;proguard-rules.pro&apos;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    externalNativeBuild &#123;</span><br><span class="line">        cmake &#123;</span><br><span class="line">            path &quot;CMakeLists.txt&quot;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>####4. 配置CMakeLists.txt文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">cmake_minimum_required(VERSION 3.4.1)</span><br><span class="line"></span><br><span class="line">find_library( log-lib</span><br><span class="line">              log )</span><br><span class="line"></span><br><span class="line">set(distribution_DIR ../../../../libs)</span><br><span class="line"></span><br><span class="line">add_library( native-lib</span><br><span class="line">             SHARED</span><br><span class="line">             src/main/cpp/native-lib.cpp )</span><br><span class="line"></span><br><span class="line">add_library( avcodec-57</span><br><span class="line">             SHARED</span><br><span class="line">             IMPORTED)</span><br><span class="line">set_target_properties( avcodec-57</span><br><span class="line">                       PROPERTIES IMPORTED_LOCATION</span><br><span class="line">                       $&#123;distribution_DIR&#125;/armeabi/libavcodec-57.so)</span><br><span class="line"></span><br><span class="line">add_library( avfilter-6</span><br><span class="line">             SHARED</span><br><span class="line">             IMPORTED)</span><br><span class="line">set_target_properties( avfilter-6</span><br><span class="line">                       PROPERTIES IMPORTED_LOCATION</span><br><span class="line">                       $&#123;distribution_DIR&#125;/armeabi/libavfilter-6.so)</span><br><span class="line"></span><br><span class="line">add_library( avformat-57</span><br><span class="line">             SHARED</span><br><span class="line">             IMPORTED)</span><br><span class="line">set_target_properties( avformat-57</span><br><span class="line">                       PROPERTIES IMPORTED_LOCATION</span><br><span class="line">                       $&#123;distribution_DIR&#125;/armeabi/libavformat-57.so)</span><br><span class="line"></span><br><span class="line">add_library( avutil-55</span><br><span class="line">             SHARED</span><br><span class="line">             IMPORTED)</span><br><span class="line">set_target_properties( avutil-55</span><br><span class="line">                       PROPERTIES IMPORTED_LOCATION</span><br><span class="line">                       $&#123;distribution_DIR&#125;/armeabi/libavutil-55.so)</span><br><span class="line"></span><br><span class="line">add_library( swresample-2</span><br><span class="line">             SHARED</span><br><span class="line">             IMPORTED)</span><br><span class="line">set_target_properties( swresample-2</span><br><span class="line">                       PROPERTIES IMPORTED_LOCATION</span><br><span class="line">                       $&#123;distribution_DIR&#125;/armeabi/libswresample-2.so)</span><br><span class="line"></span><br><span class="line">add_library( swscale-4</span><br><span class="line">             SHARED</span><br><span class="line">             IMPORTED)</span><br><span class="line">set_target_properties( swscale-4</span><br><span class="line">                       PROPERTIES IMPORTED_LOCATION</span><br><span class="line">                       $&#123;distribution_DIR&#125;/armeabi/libswscale-4.so)</span><br><span class="line"></span><br><span class="line">include_directories(libs/include)</span><br><span class="line"></span><br><span class="line">target_link_libraries( native-lib</span><br><span class="line">                       avcodec-57</span><br><span class="line">                       avfilter-6</span><br><span class="line">                       avformat-57</span><br><span class="line">                       avutil-55</span><br><span class="line">                       swresample-2</span><br><span class="line">                       swscale-4</span><br><span class="line">                       $&#123;log-lib&#125; )</span><br></pre></td></tr></table></figure></p>
<p>配置命令解释：</p>
<ul>
<li>cmake_minimum_required(VERSION 3.4.1):表示cmake的最低版本是3.4.1。 </li>
<li>add_library():添加库，分为两种，一种是需要编译为库的代码，一种是已经编译好的库文件。<br>  1). 比如上面编译好的ffmpeg的库.<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">add_library( avutil-55</span><br><span class="line">             SHARED</span><br><span class="line">             IMPORTED)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>  这里avutil-55表示库的名称，SHARED表示是共享库，一般.so文件，还有STATIC，一般.a文件。IMPORTED表示引用的不是生成的。<br>  2). 用到的需要生产的.so文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">add_library( native-lib</span><br><span class="line">             SHARED</span><br><span class="line">             src/main/cpp/native-lib.cpp )</span><br></pre></td></tr></table></figure></p>
<p>  最后的参数是源码的路径，如果有更多的源码就接下去写上。</p>
<ul>
<li>set_target_properties：对于已经编译好的so文件需要引入，所以需要设置。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">set_target_properties( avutil-55</span><br><span class="line">                       PROPERTIES IMPORTED_LOCATION</span><br><span class="line">                       ../../../../libs/armeabi-v7a/libavutil-55.so )</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>  这里avutil-55是名字，然后是PROPERTIES IMPORTED_LOCATION加上库的路径。 </p>
<ul>
<li><p>include_directories：一般外面引入的库文件需要头文件，所以可以通过这个来引入：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">include_directories(libs/include)</span><br></pre></td></tr></table></figure>
</li>
<li><p>target_link_libraries：链接，把需要的so文件链接起来，这里native-lib需要链接ffmpeg的库文件。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">target_link_libraries( native-lib</span><br><span class="line">                 avcodec-57</span><br><span class="line">                 avfilter-6</span><br><span class="line">                 avformat-57</span><br><span class="line">                 avutil-55</span><br><span class="line">                 swresample-2</span><br><span class="line">                 swscale-4</span><br><span class="line">                 $&#123;log-lib&#125; )</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>####5. 先编译运行一下，如果有错误就先修改错误。</p>
<p>####6. 编写代码<br>如果你进入这一步说明之前的步骤已经没有任何问题，MainActivity.java内容如下:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">public class MainActivity extends AppCompatActivity &#123;</span><br><span class="line"></span><br><span class="line">    // Used to load the &apos;native-lib&apos; library on application startup.</span><br><span class="line">    static &#123;</span><br><span class="line">        System.loadLibrary(&quot;native-lib&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @InjectView(R.id.tv_info)</span><br><span class="line">    TextView mTvInfo;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    protected void onCreate(Bundle savedInstanceState) &#123;</span><br><span class="line">        super.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_main);</span><br><span class="line">        // 使用了ButterKnife框架</span><br><span class="line">        ButterKnife.inject(this);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @OnClick(&#123;R.id.btn_protocol, R.id.btn_format, R.id.btn_codec, R.id.btn_filter&#125;)</span><br><span class="line">    public void onViewClicked(View view) &#123;</span><br><span class="line">        switch (view.getId()) &#123;</span><br><span class="line">            case R.id.btn_protocol:</span><br><span class="line">                mTvInfo.setText(urlprotocolinfo());</span><br><span class="line">                break;</span><br><span class="line">            case R.id.btn_format:</span><br><span class="line">                mTvInfo.setText(avformatinfo());</span><br><span class="line">                break;</span><br><span class="line">            case R.id.btn_codec:</span><br><span class="line">                mTvInfo.setText(avcodecinfo());</span><br><span class="line">                break;</span><br><span class="line">            case R.id.btn_filter:</span><br><span class="line">                mTvInfo.setText(avfilterinfo());</span><br><span class="line">                break;</span><br><span class="line">            default:</span><br><span class="line">                break;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * A native method that is implemented by the &apos;native-lib&apos; native library,</span><br><span class="line">     * which is packaged with this application.</span><br><span class="line">     */</span><br><span class="line">//    public native String stringFromJNI();</span><br><span class="line"></span><br><span class="line">    public native String urlprotocolinfo();</span><br><span class="line">    public native String avformatinfo();</span><br><span class="line">    public native String avcodecinfo();</span><br><span class="line">    public native String avfilterinfo();</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>activity_main.xml<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;LinearLayout</span><br><span class="line">    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    android:id=&quot;@+id/activity_main&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:orientation=&quot;vertical&quot;&gt;</span><br><span class="line"></span><br><span class="line">    &lt;LinearLayout</span><br><span class="line">        android:layout_width=&quot;match_parent&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:orientation=&quot;horizontal&quot;&gt;</span><br><span class="line"></span><br><span class="line">        &lt;Button</span><br><span class="line">            android:id=&quot;@+id/btn_protocol&quot;</span><br><span class="line">            android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_margin=&quot;2dp&quot;</span><br><span class="line">            android:text=&quot;Protocol&quot;</span><br><span class="line">            android:textAllCaps=&quot;false&quot;/&gt;</span><br><span class="line"></span><br><span class="line">        &lt;Button</span><br><span class="line">            android:id=&quot;@+id/btn_format&quot;</span><br><span class="line">            android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_margin=&quot;2dp&quot;</span><br><span class="line">            android:text=&quot;Format&quot;</span><br><span class="line">            android:textAllCaps=&quot;false&quot;/&gt;</span><br><span class="line"></span><br><span class="line">        &lt;Button</span><br><span class="line">            android:id=&quot;@+id/btn_codec&quot;</span><br><span class="line">            android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_margin=&quot;2dp&quot;</span><br><span class="line">            android:text=&quot;Codec&quot;</span><br><span class="line">            android:textAllCaps=&quot;false&quot;/&gt;</span><br><span class="line"></span><br><span class="line">        &lt;Button</span><br><span class="line">            android:id=&quot;@+id/btn_filter&quot;</span><br><span class="line">            android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">            android:layout_margin=&quot;2dp&quot;</span><br><span class="line">            android:text=&quot;Filter&quot;</span><br><span class="line">            android:textAllCaps=&quot;false&quot;/&gt;</span><br><span class="line">    &lt;/LinearLayout&gt;</span><br><span class="line"></span><br><span class="line">    &lt;ScrollView</span><br><span class="line">        android:layout_width=&quot;match_parent&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;&gt;</span><br><span class="line"></span><br><span class="line">        &lt;TextView</span><br><span class="line">            android:id=&quot;@+id/tv_info&quot;</span><br><span class="line">            android:layout_width=&quot;match_parent&quot;</span><br><span class="line">            android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">            android:text=&quot;Hello World!&quot;/&gt;</span><br><span class="line">    &lt;/ScrollView&gt;</span><br><span class="line"></span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure></p>
<p>####7. 编写native-lib.cpp中的内容<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;jni.h&gt;</span><br><span class="line">#include &lt;string&gt;</span><br><span class="line">#include &lt;android/log.h&gt;</span><br><span class="line"></span><br><span class="line">extern &quot;C&quot; &#123;</span><br><span class="line">//编码</span><br><span class="line">#include &quot;libavcodec/avcodec.h&quot;</span><br><span class="line">//封装格式处理</span><br><span class="line">#include &quot;libavformat/avformat.h&quot;</span><br><span class="line">//过滤器</span><br><span class="line">#include &quot;libavfilter/avfilter.h&quot;</span><br><span class="line"></span><br><span class="line">#define FFLOGI(FORMAT, ...) __android_log_print(ANDROID_LOG_INFO,&quot;ffmpeg&quot;,FORMAT,##__VA_ARGS__);</span><br><span class="line">#define FFLOGE(FORMAT, ...) __android_log_print(ANDROID_LOG_ERROR,&quot;ffmpeg&quot;,FORMAT,##__VA_ARGS__);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">JNIEXPORT jstring JNICALL</span><br><span class="line">Java_com_mazaiting_ffmpegtest_MainActivity_urlprotocolinfo(JNIEnv *env, jobject instance) &#123;</span><br><span class="line"></span><br><span class="line">    char info[40000] = &#123;0&#125;;</span><br><span class="line">    av_register_all();</span><br><span class="line"></span><br><span class="line">    struct URLProtocol *pup = NULL;</span><br><span class="line"></span><br><span class="line">    struct URLProtocol **p_temp = &amp;pup;</span><br><span class="line">    avio_enum_protocols((void **) p_temp, 0);</span><br><span class="line"></span><br><span class="line">    while ((*p_temp) != NULL) &#123;</span><br><span class="line">        sprintf(info, &quot;%sInput: %s\n&quot;, info, avio_enum_protocols((void **) p_temp, 0));</span><br><span class="line">    &#125;</span><br><span class="line">    pup = NULL;</span><br><span class="line">    avio_enum_protocols((void **) p_temp, 1);</span><br><span class="line">    while ((*p_temp) != NULL) &#123;</span><br><span class="line">        sprintf(info, &quot;%sInput: %s\n&quot;, info, avio_enum_protocols((void **) p_temp, 1));</span><br><span class="line">    &#125;</span><br><span class="line">    return env-&gt;NewStringUTF(info);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">JNIEXPORT jstring JNICALL</span><br><span class="line">Java_com_mazaiting_ffmpegtest_MainActivity_avformatinfo(JNIEnv *env, jobject instance) &#123;</span><br><span class="line">    char info[40000] = &#123;0&#125;;</span><br><span class="line"></span><br><span class="line">    av_register_all();</span><br><span class="line"></span><br><span class="line">    AVInputFormat *if_temp = av_iformat_next(NULL);</span><br><span class="line">    AVOutputFormat *of_temp = av_oformat_next(NULL);</span><br><span class="line">    while (if_temp != NULL) &#123;</span><br><span class="line">        sprintf(info, &quot;%sInput: %s\n&quot;, info, if_temp-&gt;name);</span><br><span class="line">        if_temp = if_temp-&gt;next;</span><br><span class="line">    &#125;</span><br><span class="line">    while (of_temp != NULL) &#123;</span><br><span class="line">        sprintf(info, &quot;%sOutput: %s\n&quot;, info, of_temp-&gt;name);</span><br><span class="line">        of_temp = of_temp-&gt;next;</span><br><span class="line">    &#125;</span><br><span class="line">    return env-&gt;NewStringUTF(info);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">JNIEXPORT jstring JNICALL</span><br><span class="line">Java_com_mazaiting_ffmpegtest_MainActivity_avcodecinfo(JNIEnv *env, jobject instance) &#123;</span><br><span class="line">    char info[40000] = &#123;0&#125;;</span><br><span class="line"></span><br><span class="line">    av_register_all();</span><br><span class="line"></span><br><span class="line">    AVCodec *c_temp = av_codec_next(NULL);</span><br><span class="line"></span><br><span class="line">    while (c_temp != NULL) &#123;</span><br><span class="line">        if (c_temp-&gt;decode != NULL) &#123;</span><br><span class="line">            sprintf(info, &quot;%sdecode:&quot;, info);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            sprintf(info, &quot;%sencode:&quot;, info);</span><br><span class="line">        &#125;</span><br><span class="line">        switch (c_temp-&gt;type) &#123;</span><br><span class="line">            case AVMEDIA_TYPE_VIDEO:</span><br><span class="line">                sprintf(info, &quot;%s(video):&quot;, info);</span><br><span class="line">                break;</span><br><span class="line">            case AVMEDIA_TYPE_AUDIO:</span><br><span class="line">                sprintf(info, &quot;%s(audio):&quot;, info);</span><br><span class="line">                break;</span><br><span class="line">            default:</span><br><span class="line">                sprintf(info, &quot;%s(other):&quot;, info);</span><br><span class="line">                break;</span><br><span class="line">        &#125;</span><br><span class="line">        sprintf(info, &quot;%s[%10s]\n&quot;, info, c_temp-&gt;name);</span><br><span class="line">        c_temp = c_temp-&gt;next;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    return env-&gt;NewStringUTF(info);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">JNIEXPORT jstring JNICALL</span><br><span class="line">Java_com_mazaiting_ffmpegtest_MainActivity_avfilterinfo(JNIEnv *env, jobject instance) &#123;</span><br><span class="line">    char info[40000] = &#123;0&#125;;</span><br><span class="line">    avfilter_register_all();</span><br><span class="line"></span><br><span class="line">    AVFilter *f_temp = (AVFilter *)avfilter_next(NULL);</span><br><span class="line">    while(f_temp != NULL) &#123;</span><br><span class="line">        sprintf(info, &quot;%s%s\n&quot;, info, f_temp-&gt;name);</span><br><span class="line">        f_temp = f_temp-&gt;next;</span><br><span class="line">    &#125;</span><br><span class="line">    return env-&gt;NewStringUTF(info);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">jstring</span><br><span class="line">Java_com_mazaiting_ffmpegtest_MainActivity_stringFromJNI(</span><br><span class="line">        JNIEnv *env,</span><br><span class="line">        jobject /* this */) &#123;</span><br><span class="line">    std::string hello = &quot;Hello from C++&quot;;</span><br><span class="line">    return env-&gt;NewStringUTF(hello.c_str());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>注：所有的包含C代码的方法都必须版含在extern “C”{}中</strong></p>
<p>####运行结果：<br><img src="http://upload-images.jianshu.io/upload_images/3110861-d1168eca09143bc6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"></p>
<p>####参考文章:</p>
<ol>
<li><a href="http://lib.csdn.net/article/liveplay/49953?knId=1586" target="_blank" rel="noopener">Android Studio cmake编译ffmpeg</a></li>
</ol>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/FFmpeg/Android-Studio-解决：error-adding-symbols--File-in-wrong-format---clang++/" rel="next" title="Android-Studio-解决：error-adding-symbols--File-in-wrong-format---clang++">
                <i class="fa fa-chevron-left"></i> Android-Studio-解决：error-adding-symbols--File-in-wrong-format---clang++
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/FFmpeg/Android-使用-FFmpeg命令行(多包)/" rel="prev" title="Android-使用-FFmpeg命令行(多包)">
                Android-使用-FFmpeg命令行(多包) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
